package com.zdkj.subway.stm.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.stm.entity.SwStConductorAdvancePayment;
import com.zdkj.subway.stm.entity.SwStConductorSellDetail;
import com.zdkj.subway.stm.mapper.SwStConductorAdvancePaymentMapper;
import com.zdkj.subway.stm.service.SwStConductorAdvancePaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 售票员预收款
 *
 * @author songtuo
 * @date 2020-07-11 21:38:12
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swstconductoradvancepayment" )
@Api(value = "swstconductoradvancepayment", tags = "售票员预收款管理")
@Slf4j
public class SwStConductorAdvancePaymentController {

    private final  SwStConductorAdvancePaymentService swStConductorAdvancePaymentService;

    @Autowired
    SwStConductorAdvancePaymentMapper swStConductorAdvancePaymentMapper;

    /**
     * 分页查询
     * @param page 分页对象
     * @param swStConductorAdvancePayment 售票员预收款
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getSwStConductorAdvancePaymentPage(Page page, SwStConductorAdvancePayment swStConductorAdvancePayment, String handTimeDTO) {
        SwStConductorAdvancePayment swStConductorAdvancePaymentObj = ObjectUtils.getDefaultObj(SwStConductorAdvancePayment.class);
        swStConductorAdvancePayment.setLineCode(swStConductorAdvancePaymentObj.getLineCode());
        swStConductorAdvancePayment.setStationCode(swStConductorAdvancePaymentObj.getStationCode());
        return R.ok(swStConductorAdvancePaymentService.page(page, Wrappers.query(swStConductorAdvancePayment)
                .lambda()
                .likeRight(handTimeDTO != null, SwStConductorAdvancePayment::getHandTime, handTimeDTO)));
    }


    /**
     * 通过id查询售票员预收款
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(swStConductorAdvancePaymentService.getById(id));
    }

    /**
     * 新增售票员预收款
     * @param swStConductorAdvancePayment 售票员预收款
     * @return R
     */
    @ApiOperation(value = "新增售票员预收款", notes = "新增售票员预收款")
    @SysLog("新增售票员预收款" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('swstconductoradvancepayment_add')" )
    public R save(@RequestBody SwStConductorAdvancePayment swStConductorAdvancePayment) {
        SwStConductorAdvancePayment advances  = swStConductorAdvancePaymentMapper.getConductorAdvancePayment(swStConductorAdvancePayment);
        if(advances!=null){
            throw new RuntimeException("请检查售票员" + swStConductorAdvancePayment.getConductorPersonName() + "在" +swStConductorAdvancePayment.getHandTime().toLocalDate() + "的数据已存在,请勿重复添加！");
        }
        SwStConductorAdvancePayment swStConductorAdvancePaymentObj = ObjectUtils.getDefaultObj(SwStConductorAdvancePayment.class);
        BeanUtil.copyProperties(swStConductorAdvancePaymentObj, swStConductorAdvancePayment, CopyOptions.create().setIgnoreNullValue(true));
        return R.ok(swStConductorAdvancePaymentService.save(swStConductorAdvancePayment));
    }

    /**
     * 修改售票员预收款
     * @param swStConductorAdvancePayment 售票员预收款
     * @return R
     */
    @ApiOperation(value = "修改售票员预收款", notes = "修改售票员预收款")
    @SysLog("修改售票员预收款" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('swstconductoradvancepayment_edit')" )
    public R updateById(@RequestBody SwStConductorAdvancePayment swStConductorAdvancePayment) {
        return R.ok(swStConductorAdvancePaymentService.updateById(swStConductorAdvancePayment));
    }

    /**
     * 通过id删除售票员预收款
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除售票员预收款", notes = "通过id删除售票员预收款")
    @SysLog("通过id删除售票员预收款" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('swstconductoradvancepayment_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(swStConductorAdvancePaymentService.removeById(id));
    }


    /**
     * 通过id批量删除售票员预收款
     * @param idList id
     * @return R
     */
    @ApiOperation(value = "通过id批量删除售票员预收款", notes = "通过id批量删除售票员预收款")
    @SysLog("通过id批量删除售票员预收款")
    @DeleteMapping("/batchDelete")
    @Transactional
    @PreAuthorize("@pms.hasPermission('swstconductoradvancepayment_batch_del')")
    public R batchRemoveById(@RequestBody List<Integer> idList) {
        List<SwStConductorAdvancePayment> list = swStConductorAdvancePaymentService.list(new QueryWrapper<SwStConductorAdvancePayment>().lambda().in(SwStConductorAdvancePayment::getId, idList));
        list.forEach(swStConductorAdvancePaymentService::removeById);
        return R.ok();
    }

    @ApiOperation(value = "导出售票员上交预收款数据", notes = "导出售票员上交预收款数据")
    @GetMapping("/export")
    public void export(Page page, SwStConductorAdvancePayment stConductorAdvancePayment, HttpServletResponse response) {
        List<SwStConductorAdvancePayment> records = swStConductorAdvancePaymentService.page(page, Wrappers.query(stConductorAdvancePayment)).getRecords();
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("售票员上交预收款数据", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + System.currentTimeMillis() + ".xlsx");
            EasyExcel.write(response.getOutputStream(), SwStConductorAdvancePayment.class).
                    sheet("售票员上交预收款数据").doWrite(records);
        } catch (IOException e) {
            log.error("导出售票员上交预收款数据失败");
        }
    }

}
